package com.sky.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.po.OrdersPO;
import com.sky.po.UserPO;
import com.sky.repository.OrderRepository;
import com.sky.repository.UserRepository;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.DateFormatter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @Content:
 * @Author: Asen
 * @Date: 2023/9/26 20:17
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private UserRepository userRepository;
    /**
     * 统计指定时间区间内的营业额数据
     * @param begin
     * @param end 当前日期的前一天
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverReportVO(LocalDate begin, LocalDate end) {
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);//获取该日期当天23:59:59
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);//获取该日期当天00:00:00
        List<LocalDate> time = new ArrayList<>();
        List<String> amount = new ArrayList<>();
        List<HashMap<String, Object>> report = orderRepository.selectReport(beginTime, endTime);
        Map<String, String> strMap = new HashMap<>();
        report.forEach(s->{
            strMap.put(s.get("day").toString(),s.get("amount").toString());
        });
        while (!begin.equals(end)){
            time.add(begin);
            String s = strMap.get(begin.toString());
            amount.add(s!=null?s:"0.0");
            begin = begin.plusDays(1);
        }
        return new TurnoverReportVO(time.toString(),amount.toString());
    }

    /**
     * 统计指定时间区间内的用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);//获取该日期当天23:59:59
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);//获取该日期当天00:00:00
        List<HashMap<String, String>> addUser = userRepository.selectReport(beginTime, endTime);
        int beforeUser = userRepository.selectCount(new LambdaQueryWrapper<UserPO>()
                .le(UserPO::getCreateTime, beginTime)).intValue();
        Map<String,Object> strMap = new HashMap<>();
        addUser.forEach(s->strMap.put(s.get("day"),s.get("count")));
        List<LocalDate> time = new ArrayList<>();
        List<String> totalUserList = new ArrayList<>();
        List<String> newUserList = new ArrayList<>();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        while(!end.plusDays(1).equals(begin)){
            time.add(begin);
            String s = "0";
            if(ObjectUtil.isNotNull(strMap.get(begin.toString()))){
                s = strMap.get(begin.toString()).toString();
            }
            begin = begin.plusDays(1);
            newUserList.add(s);
            totalUserList.add((beforeUser+=Integer.parseInt(s))+"");
        }
        return new UserReportVO(time.toString(),totalUserList.toString(),newUserList.toString());
    }
}
